package com.inspur.his.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inspur.his.pojo.RenShi;
import com.inspur.his.service.RenShiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;

/*人事管理的全部servlet*/
@Controller
public class RenShiController {
    //这里调用service类
    @Autowired
    RenShiService renShiService;

    //人事条件查询+分页
    @RequestMapping("DoctorListController")//访问地址
    public String RenSshiList(HttpServletRequest request, Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) throws UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //获取页面提交来的查询条件，来自前端页面查询部分标签的 name属性
        String departId = request.getParameter("departId");//病人姓名
        //  点击用户管理左侧菜单进行默认无条件查询
        HashMap map = new HashMap();
        map.put("departId",departId);
        PageHelper.startPage(pn,5);
        List<RenShi> renShiList = renShiService.renShiInList(map);
        /*这里pageInfo.list就能获得分页完的数据*/
        PageInfo<RenShi> pageInfo = new PageInfo<RenShi>(renShiList,renShiList.size());
        /*Model主要用于传值给jsp*/
        model.addAttribute("renShiList",renShiList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("departId",departId);

        HttpSession s = request.getSession(true);
        s.setAttribute("renShiList",renShiList);
        s.setAttribute("pageInfo",pageInfo);
        s.setAttribute("departId",departId);



        return "RenShi/RenShiGuanLi.jsp";
    }
    //初始先跳到save页面
    @RequestMapping("DoctorInitInsertController")//访问地址
    public String MemberInitInsert(HttpServletRequest request,Model model) throws UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //跳转到添加页面
        return "RenShi/RenShiInsert.jsp";
    }
    //添加
    @RequestMapping("DoctorSaveController")//访问地址
    public String RenShiSave(HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //2.接受页面提交的数据
        String DEPARTID = request.getParameter("departId");
        String DOCID = request.getParameter("docId");
        String REALNAME = request.getParameter("docName");
        String SEX = request.getParameter("docSex");
        String IDcard = request.getParameter("idcard");
        String POSITIONAL = request.getParameter("positional");//职称
        String Cposition = request.getParameter("cposition");//职务
        String address = request.getParameter("address");//籍贯
        String TEL = request.getParameter("docTel");
        //将接受的数据封装
         RenShi renShi =new RenShi();
         renShi.setDocid(DOCID);
//         renShi.setCreatedate(new Date());
         renShi.setDepartid(DEPARTID);
         renShi.setRealname(REALNAME);
         renShi.setSex(SEX);
         renShi.setPositional(POSITIONAL);
         renShi.setIdcard(IDcard);
         renShi.setCposition(Cposition);
         renShi.setAddress(address);
         renShi.setTel(TEL);
         renShi.setRole("04");


        //调用service保存
        //这里不要new新的service层，会导致mapper接口null
        renShiService.save(renShi);

        return "redirect:DoctorListController";
    }

    @RequestMapping("DoctordeleteController")//访问地址
    public String RenShidelete(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        String docId = request.getParameter("id");
        renShiService.delete(docId);

        return "redirect:DoctorListController";
    }
   /* //修改医生信息(现根据id查询，再根据id修改,最后跳到列表页面)
    @RequestMapping("DoctorInController")//访问地址
    public String RenShiIn(HttpServletRequest request) throws UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //获取目标医生id
        String docId = request.getParameter("id");//医生docId
        //找到并修改status为1（表示住院了）
        //条件对象
        RenShi renShi =new RenShi();
        renShi.setDocid(docId);
        //调用查找
        RenShi renShi2 = renShiService.findById(renShi);
        //根据返回的对象，再修改
        renShiService.updateRenShi(renShi2);

        return "redirect:MemberListController";
    }
    //修改信息
    @RequestMapping("DoctorFindByIdController")//访问地址
    public String RenShiFindById(HttpServletRequest request,Model model) throws ParseException, UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //获取目标医生id
        String docId = request.getParameter("docId");
        RenShi renShi =new RenShi();
        renShi.setDocid(docId);

        RenShi renShi2 = renShiService.findById(renShi);//id是主键id
        *//*Member member2 = memberService.findById(member);//id是主键id*//*

        model.addAttribute("renShi2",renShi2);

        return "ZhuYuan/RenShiupdate.jsp";
    }*/



}
